package com.scpii.universal.pns.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.scpii.universal.action.BaseAction;
import com.scpii.universal.pns.entity.AppConfigs;
import com.scpii.universal.pns.service.AppConfigService;

@Controller
@Scope("prototype")
public class AccountManageAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7752633605678709813L;

	private Map<String, String> configs;

	private String params;

	@Resource
	private AppConfigService appconfigsService;

	public String getActiveList() {
		configs = appconfigsService.getAllConfigs(getUserInfo().getAppId());
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public void editAccount() throws IOException {
		HttpServletResponse response = getResponse();
		PrintWriter out = response.getWriter();
		try {
			JSONArray jsonArray = JSONArray.fromObject(params);
			List<AppConfigs> appConfigs = JSONArray.toList(jsonArray,
					AppConfigs.class);
			appconfigsService.updateConfigs(appConfigs, getUserInfo()
					.getAppId());
			out.print("success");
		} catch (Exception e) {
			out.print("error");
		} finally {
			out.flush();
			out.close();
		}
	}

	public void delAccount() throws IOException {
		HttpServletResponse response = getResponse();
		PrintWriter out = response.getWriter();
		try {
			appconfigsService.delConfigs(params.split(","), getUserInfo()
					.getAppId());
			out.print("success");
		} catch (Exception e) {
			out.print("error");
		} finally {
			out.flush();
			out.close();
		}
	}

	public Map<String, String> getConfigs() {
		return configs;
	}

	public void setConfigs(Map<String, String> configs) {
		this.configs = configs;
	}

	public String getParams() {
		return params;
	}

	public void setParams(String params) {
		this.params = params;
	}

}
